package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Slf4j
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Transactional
    @Override
    public void insert(SetmealDTO dto) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto, setmeal);
        setmealMapper.insert(setmeal);
        log.info("获取到的id：{}", setmeal.getId());
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmeal.getId()));
        setmealDishMapper.insert(setmealDishes);
    }

    @Override
    public PageResult page(SetmealPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        Page<SetmealVO> page = setmealMapper.page(dto);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Transactional
    @Override
    public void delete(List<Long> ids) {
        //搜索ids中，status状态为启用的总计数，大于0个抛出异常
        Integer count = setmealMapper.getStatusCount(ids, StatusConstant.ENABLE);
        if (count > 0) {
            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        }
        setmealMapper.delete(ids);
        setmealDishMapper.deleteBySetmeaslId(ids);
    }

    @Override
    public void status(Integer status, Long id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setId(id);
        if (status == StatusConstant.DISABLE) {
            //改成：DISABLE
            setmeal.setStatus(StatusConstant.DISABLE);
        } else if (status == StatusConstant.ENABLE) {
            //改成：ENABLE
            Integer count = setmealDishMapper.getDishsStatusCountBySetmealId(id, StatusConstant.DISABLE);
            if (count > 0) {
                throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
            setmeal.setStatus(StatusConstant.ENABLE);
        }
        setmealMapper.update(setmeal);
    }

    @Override
    public SetmealVO getSetmealVOById(Long id) {
        SetmealVO vo = new SetmealVO();
        Setmeal setmeal = setmealMapper.getSetmealById(id);
        BeanUtils.copyProperties(setmeal, vo);
        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);
        vo.setSetmealDishes(setmealDishes);
        return vo;
    }

    @Transactional
    @Override
    public void update(SetmealDTO dto) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto,setmeal);
        setmealMapper.update(setmeal);
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        setmealDishMapper.deleteBySetmeaslId(Collections.singletonList(setmeal.getId()));
        Long id = setmeal.getId();
        setmealDishes.forEach(item -> item.setSetmealId(id));
        setmealDishMapper.insert(setmealDishes);
    }

}
